// frame.js - 浏览器全局，无 export
function buildFrame(modelId, pressCount, depth, pos, rate, maxDepth, recoil,
                    ventCount, ventVol, airway, ventDur, maxVent) {
  const buf = new Uint8Array(20);
  buf[0]  = 0x5A;
  buf[1]  = modelId[0];
  buf[2]  = modelId[1];
  buf[3]  = modelId[2];
  buf[4]  = 0x01;
  buf[5]  = 0x0C;
  buf[6]  = (pressCount >> 8) & 0xFF;
  buf[7]  = pressCount & 0xFF;
  buf[8]  = depth;
  buf[9]  = pos;
  buf[10] = rate;
  buf[11] = maxDepth;
  buf[12] = recoil;
  buf[13] = ventCount;
  buf[14] = Math.floor(ventVol / 10);
  buf[15] = airway;
  buf[16] = Math.floor(ventDur * 10);
  buf[17] = Math.floor(maxVent / 10);
  let cs = 0;
  for (let i = 1; i <= 17; i++) cs ^= buf[i];
  buf[18] = cs;
  buf[19] = 0xA5;
  return buf;
}

function buildCommand(modelId, func, reserve = 0x00) {
  const buf = new Uint8Array(8);
  buf[0] = 0x5A;
  buf[1] = modelId[0];
  buf[2] = modelId[1];
  buf[3] = modelId[2];
  buf[4] = func;
  buf[5] = reserve;
  let cs = 0;
  for (let i = 1; i <= 5; i++) cs ^= buf[i];
  buf[6] = cs;
  buf[7] = 0xA5;
  return buf;
}

/* ========== 万能导出 ========== */
if (typeof module !== 'undefined' && module.exports) {
  // Node 环境
  module.exports = { buildFrame, buildCommand };
} else {
  // 浏览器环境 - 挂到全局 window
  window.buildFrame = buildFrame;
  window.buildCommand = buildCommand;
}